/*
Copyright 2010 Jacob Wiltse

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/*
 * Please note this is very unfinished, is still buggy,
 * and doesn't have full functionality.
 * If you would like to help with the project, send an email to jmwiltse@mtu.edu
 */

/*
 * TODO: Add support to act as login shell rather than strictly interactive
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>

int isBuiltIn(char *cmd);
char **parseCmd(char *cmdline);

int main (int argc, char **argv)
{
    int child, i;
    char *dir = NULL;
    char *input = NULL;
    char **cmd;
    int *ret, *isLogin;
    char **command;
    if(argc > 0){
        for(i = 0; i < argc; i++){
            if(argv[i] == "-v")
                printf("nsh 0.1\n");
        }
    }
    while (1)
    {
        dir = get_current_dir_name();
        dir = realloc(dir, strlen(dir)*sizeof(char) + 3);
        if(dir == NULL){
            printf("Shit just went wrong, exiting..."); // This shouldn't happen
            return(11);
        }
        // Using GNU Readline for tab completion and history
        // You will need the readline on your system for compilation
        input = readline(strcat(dir, "# "));
        if(input && *input){
            // Add command to command history
            add_history(input);
            // Parse the commmand to something we can pass to execvp
            cmd = parseCmd(input);
            // Test if it is built in and execute
            if(isBuiltIn(cmd[0]) == 0){
                if(!strcmp(cmd[0], "exit")) exit(1);
                if(!strcmp(cmd[0], "cd")) chdir(cmd[1]);
            }
            else{
                // Fork the process
                child = fork();
                // Parent thread sleeps while child works
                if(child)
                    child = wait(ret);
                // Child executes command
                else{
                    if(execvp(cmd[0], cmd)) {
                        printf("%s\n", strerror(errno));
                        exit(12);
                    }
                }
            }
            // Free up manually allocated memory
            // readline manually allocated input, must be freed
            free(cmd);
            free(input);
        }
    }
    return 0;
}

// Test if the command is built in
// TODO: move execution of commands to this function
int isBuiltIn(char* cmd){
    if(!strcmp(cmd, "exit") || !strcmp(cmd, "cd"))
        return 0;
    else
        return 1;
}

// Parse the command
char **parseCmd(char *cmdline){
    char** tok = (char**)malloc(strlen(cmdline)*sizeof(char*));
    int x = 0;
    tok[x] = strtok(cmdline, " ");
    while(tok[x] != NULL){
        x++;
        tok[x] = strtok(NULL, " ");
    }
    return tok;
}
